package com.alex.utils.http;

import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class HttpSession {

	private HttpContext httpContext;

	private CookieStore cookieStore;

	private HttpSession(HttpContext httpContext, CookieStore cookieStore) {
		this.httpContext = httpContext;
		this.cookieStore = cookieStore;
	}

	public static HttpSession createSession() {
		CookieStore cookieStore = new BasicCookieStore();
		HttpContext httpContext = new BasicHttpContext();
		httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

		return new HttpSession(httpContext, cookieStore);
	}

	public HttpContext getHttpContext() {
		return httpContext;
	}

	public void setHttpContext(HttpContext httpContext) {
		this.httpContext = httpContext;
	}

	public CookieStore getCookieStore() {
		return cookieStore;
	}

	public void setCookieStore(CookieStore cookieStore) {
		this.cookieStore = cookieStore;
	}

	public boolean containsNotEmptyCookie(String name) {
		for (Cookie cookie : cookieStore.getCookies()) {
			if (name.equals(cookie.getName()) && !cookie.getValue().isEmpty())
				return true;
		}
		return false;
	}
}
